//
//  MainPageViewController.swift
//  DollarBuy
//
//  Created by 郑东喜 on 2016/11/6.
//  Copyright © 2016年 郑东喜. All rights reserved.
//  首页轮播图

import UIKit
import WebKit


class MainPageViewController : WKBaseViewController {
    //token
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationItem.title = "首页"

        
        if mainIndexArray.count > 1 {
            //取出数组的url转为字符串
            url = mainIndexArray.lastObject as! String
            
            webView.load(URLRequest.init(url: URL.init(string: url)!))
        } else {
            DispatchQueue.main.async {
                
                let urlStr = "http://yungou.ie1e.com/app/index.aspx?devtype=1" + "&token=" + self.token
                
                //判断是否包含域名
                if urlStr.contains(comStrURL) {
                    let url1 = URL.init(string: urlStr)
                    
                    ///检测网络
                    TestNet.shared.getNetStatus(comfun: { (result) in
                        if result.rawValue == "worked" {
                            print("worked")
                            
                            DispatchQueue.main.async {
                                
                                //                            self.urlRequestCache = NSURLRequest(url: url!, cachePolicy: NSURLRequest.CachePolicy.useProtocolCachePolicy, timeoutInterval: 10)
                                self.urlRequestCache = NSURLRequest.init(url: url1!)
                                
                                
                                
                                self.webView.load(self.urlRequestCache as URLRequest)
                                
                            }
                            
                        } else {
                            
                            DispatchQueue.main.async {
                                
                                print("本地")
                                print(self.webView.bounds)
                                
                                //                            self.urlRequestCache = NSURLRequest.init(url: url!, cachePolicy: .returnCacheDataElseLoad, timeoutInterval: 0)
                                self.urlRequestCache = NSURLRequest.init(url: url1!)
                                
                                self.webView.load(self.urlRequestCache as URLRequest)
 
                            }
                            
                        }
                        
                    })
                } else {
                    CustomAlertView.shared.alertWithTitle(strTitle: "请下拉刷新")
                }
            }
        }
    }
    
    
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        
        url = (navigationAction.request.url?.absoluteString)!

        print(url)
        
        textView.text = "\((#file as NSString).lastPathComponent):(\(#line))" + url
            if navigationAction.navigationType == WKNavigationType.linkActivated {

                    print(mainIndexArray)

                        if self.url.contains("?") {
                            self.url = self.url + ("&devtype=1&token=") + (token)
                        } else {
                            self.url = self.url + ("?devtype=1&token=") + (token)
                        }

                    mainIndexArray.add(self.url)
                    self.aaa(str: mainIndexArray.lastObject as! String)
//                }
                
                decisionHandler(.cancel)
                
            } else {
                decisionHandler(.allow)
            }
        
    }
    

    //url---
    func aaa(str : String) -> Void {
        
        let vvv = MainPageViewReplaceVIew()
        vvv.url = str
        
        self.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(vvv, animated: true)
        self.hidesBottomBarWhenPushed = false
        
    }

}

